// let myHandler = async function (event, context, callback, logger) {
//   logger.info(event);
//   let resourceId: string = event.body ? JSON.parse(event.body).resourceId : event.resourceId;
//   let userId: string = event.body ? JSON.parse(event.body).userId : event.userId;
//   // do something here
//   let detailData = {
//     resourceId: '10000001',
//     type: 'feed',
//     webUrl: 'https://developer.harmonyos.com/',
//     collectionCount: 116,
//     likesCount: 668,
//     mediaSrc: 'https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/bird.mp4?token=be902a58-4f4c-4774-844b-4e21dcd0a5a2',
//     isLiked: true,
//     isCollected: false,
//     relatedSource: [
//       {
//         "id": "10000012",
//         "title": "测试数据",
//         "brief": "这是测试数据的简介",
//         "headerImageUrl": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/harmonyos-path1.png?token=e0344a82-a2c4-4988-bb27-47bbda67ae10",
//         "type": "feed",
//         "webUrl": "https://fly.harmonyos-developer.top/",
//         "publishDate": "2023-06-13T02:47:20.408Z",
//         "topics": "t001",
//         "viewsCount": 10,
//         "collectionCount": 10,
//         "likesCount": 10,
//         "tag": 1,
//         "bannerSrc": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/harmonyos-path1.png?token=e0344a82-a2c4-4988-bb27-47bbda67ae10",
//         "mediaSrc": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/bird.mp4?token=be902a58-4f4c-4774-844b-4e21dcd0a5a2",
//         "isLiked": false,
//         "isCollected": false
//       },
//       {
//         "id": "10000011",
//         "title": "测试数据",
//         "brief": "这是测试数据的简介",
//         "headerImageUrl": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/harmonyos-path1.png?token=e0344a82-a2c4-4988-bb27-47bbda67ae10",
//         "type": "feed",
//         "webUrl": "https://fly.harmonyos-developer.top/",
//         "publishDate": "2023-06-13T02:47:20.408Z",
//         "topics": "t001",
//         "viewsCount": 10,
//         "collectionCount": 10,
//         "likesCount": 10,
//         "tag": 1,
//         "bannerSrc": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/harmonyos-path1.png?token=e0344a82-a2c4-4988-bb27-47bbda67ae10",
//         "mediaSrc": "https://ops-dra.agcstorage.link/v0/my-cloud-mvc5l/bird.mp4?token=be902a58-4f4c-4774-844b-4e21dcd0a5a2",
//         "isLiked": false,
//         "isCollected": false
//       }
//     ]
//   };
//   callback({
//     code: 0,
//     message: "Success",
//     data: detailData
//   });
// };
//
// export { myHandler };
import { DatabaseHelper } from './DatabaseHelper.js';

let myHandler = async function (event, context, callback, logger) {
  logger.info(JSON.stringify(event));
  let userid: string = event.body ? JSON.parse(event.body).userId : event.userId;
  let resourceId: string = event.body ? JSON.parse(event.body).resourceId : event.resourceId;
  try {
    const databaseHelper = new DatabaseHelper(context, logger);
    const result = await databaseHelper.queryResource(userid,resourceId);
    if (result) {
      callback({
        code: 0,
        message: 'Success',
        data: result,
      });
    } else {
      callback({
        code: 1,
        message: '未找到数据',
        data: null,
      });
    }
  } catch (err) {
    logger.error("func error:" + err.message + " stack:" + err.stack);
    callback({
      code: 3,
      message: '数据查询失败',
      data: err,
    });
  }
};

export { myHandler };